<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :md="6" :sm="8">
            <a-form-item :label="$t('m.bpm.entity.myInstanceClassify')">
              <a-select v-model="queryParam.myInstanceClassify" placeholder="">
                <a-select-option value="all">{{this.$t('m.common.label.all')}}</a-select-option>
                <a-select-option v-for="item in myInstanceClassify" :key="item.value" :value="item.value">
                  {{ item.text }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="8">
            <a-form-item :label="$t('m.bpm.entity.businessTitle')">
              <a-input placeholder="" v-model="queryParam.businessTitle"></a-input>
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :md="6" :sm="8">
              <a-form-item :label="$t('m.bpm.entity.processDefName')">
                <a-input placeholder="" v-model="queryParam.processDefName"></a-input>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="8">
              <a-form-item :label="$t('m.bpm.entity.processInstanceState')">
                <a-select v-model="queryParam.processInstanceState" placeholder="">
                  <a-select-option value="ALL">{{this.$t('m.common.label.all')}}</a-select-option>
                  <a-select-option v-for="item in processInstanceState" :key="item.value" :value="item.value">
                    {{ item.text }}
                  </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
          </template>
          <a-col :md="6" :sm="8">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">{{ $t('m.common.button.query') }}</a-button>
              <a-button type="default" @click="searchReset" icon="reload" style="margin-left: 8px">{{ $t('m.common.button.reset') }}</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? this.$t('m.common.button.fold') : this.$t('m.common.button.expand') }}
                <ych-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>

        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="batchDel" type="primary" icon="delete">{{ $t('m.common.button.delete') }}</a-button>
    </div>

    <!-- table区域-begin -->
    <div>
      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        @change="handleTableChange">
        <template slot="serialNum" slot-scope="text,record,index">
          {{(ipagination.current-1) * ipagination.pageSize + parseInt(index) + 1}}
        </template>
        <span slot="action" slot-scope="text, record">
          <a @click="handleDetail(record)">{{ $t('m.common.button.open') }}</a>
          <!--<a-divider type="vertical"/>
          <a @click="handleDetail2(record)">打开2</a>
          <a-divider type="vertical"/>
          <a @click="handleDetail3(record)">打开3</a>-->
          <a-divider type="vertical" v-if="isDelete(record)"/>
          <a-popconfirm placement="left" :title="$t('m.common.tip.delete_confirm')" @confirm="() => handleDelete(record.processInstanceId)"
                        v-if="isDelete(record)">
            <a>{{ $t('m.common.button.delete') }}</a>
          </a-popconfirm>
        </span>

      </a-table>
    </div>

    <redirect-modal ref="flowForm" @refreshParent="modalFormOk"></redirect-modal>
  </a-card>
</template>

<script>

import { YcListMixin } from '@/mixins/YcListMixin'
import { deleteAction } from '@/api/manage'
import { filterMultiDictText } from '@/components/dict/YchDictSelectUtil'
import RedirectModal from './redirect/RedirectModal'
import { BpmConstant } from './BpmConstant'
import YchConstant from '@/api/YchConstant'

export default {
  name: 'MyProcessInstanceList',
  mixins: [YcListMixin],
  components: {
    RedirectModal,
  },
  data() {
    return {
      queryParam: {
        myInstanceClassify: 'start'
      },
      // delTip: '提示：只有标识为我发起，且没有提交过的流程才能删除。',
      processInstanceState: BpmConstant.processInstanceState,
      myInstanceClassify: BpmConstant.myInstanceClassify,
      // 表头
      columns: [
        {
          title:this.$t('m.common.label.number'),
          dataIndex: '',
          key: 'rowIndex',
          width: '50px',
          align: 'center',
          scopedSlots: { customRender: 'serialNum' },
        },
        {
          title: this.$t('m.bpm.entity.businessTitle'),
          align: 'left',
          dataIndex: 'businessTitle',
          ellipsis: true,
        },
        {
          title: this.$t('m.bpm.entity.processDefName'),
          align: 'left',
          width: '150px',
          dataIndex: 'processDefName',
          ellipsis: true,
        },
        {
          title: this.$t('m.bpm.entity.startTimeString'),
          align: 'center',
          dataIndex: 'startTimeString',
          width: '130px'
        },
        {
          title: this.$t('m.bpm.entity.endTimeString'),
          align: 'center',
          dataIndex: 'endTimeString',
          width: '130px'
        },
        /*{
          title: this.$t('m.bpm.entity.durationString'),
          align: "center",
          width: "100px",
          dataIndex: 'duration'
        },*/
        // {
        //   title: this.$t('m.bpm.entity.myInstanceClassify'),
        //   align: 'center',
        //   dataIndex: 'myInstanceClassify',
        //   width: '90px',
        //   customRender: (text) => {
        //     if (!text) {
        //       return ''
        //     } else {
        //       return filterMultiDictText(BpmConstant.myInstanceClassify, text)
        //     }
        //   }
        // },
        {
          title: this.$t('m.bpm.entity.startUserName'),
          align: 'center',
          dataIndex: 'startUserName',
          width: '130px',
          customRender: function(t, r, index) {
            return t + (r.startDeptName ? '/' + r.startDeptName : '')
          }
        },
        {
          title: this.$t('m.bpm.entity.processInstanceState'),
          align: 'center',
          dataIndex: 'processInstanceState',
          width: '120px',
          customRender: (text) => {
            if (!text) {
              return ''
            } else {
              return filterMultiDictText(BpmConstant.processInstanceState, text)
            }
          }
        },
        {
          title: this.$t('m.common.label.operate'),
          dataIndex: 'action',
          align: 'center',
          scopedSlots: { customRender: 'action' },
          width: '120px'
        }
      ],
      url: {
        list: YchConstant.PREFIX_BPM + '/bpm-front/myProcessInstanceRest/list/v1',
        delete: YchConstant.PREFIX_BPM + '/bpm-front/myProcessInstanceRest/delete/v1',
        deleteBatch: YchConstant.PREFIX_BPM + '/bpm-front/myProcessInstanceRest/deleteBatchForFront/v1'
      }
    }
  },
  inject: ['addRouter'],
  methods: {
    searchReset() {
      this.queryParam = {
        myInstanceClassify: 'start'
      }
      this.loadData(1)
    },
    isDelete(record) {
      return record.myInstanceClassify == 'start'
    },
    handleDetail(record) {
      let router = null
      if (this.$store.getters.formOpenType === '2') {
        router = this.addRouter({
          path: '/bpm/detailFromProcess/' + record.processInstanceId,
          component: resolve => require(['@/views/bpm/redirect/RedirectTab.vue'], resolve),
          props: {
            id: record.processInstanceId,
            func: 'detailFromProcess',
          },
          meta: {
            title: this.$t('m.bpm.common.bpmDetail'),
          }
        });
        if (router) {
          this.$router.push(router).catch(err => {})
        }
      }
      if (!router) {
        this.$refs.flowForm.detailFromProcess(record.processInstanceId)
      }
    },
    batchDel() {
      if (this.selectedRowKeys.length <= 0) {
        this.messageUtil.warning(this.$t('m.common.tip.select_one_warn'))
        return
      } else {
        let ids = ''
        for (let a = 0; a < this.selectedRowKeys.length; a++) {
          ids += this.selectedRowKeys[a] + ','
        }
        const that = this
        this.$confirm({
          title: this.$t('m.common.title.delete'),
          content: this.$t('m.common.tip.delete_confirm') + this.$t('m.bpm.verify.tip_deleteMyProcess'),
          onOk: function() {
            that.doBatchDel(ids)
          }
        })
      }
    },
    doBatchDel(ids){
      this.loading = true
      deleteAction(this.url.deleteBatch, { ids: ids }).then((res) => {
        if (res.success) {
          if (res.code === 201) {
            let { message, result: { msg, fileUrl, fileName } } = res
            let href = bootConfig[process.env.NODE_ENV].VUE_APP_API + fileUrl
            this.$warning({
              title: message,
              content: (
                < div >
                  < span > {msg} < /span><br/ >
                  < span > { this.$t('m.eform.formDesign.label.ju-ti-xiang-qing-qing') } < a href = {href} target = '_blank' download = {fileName} > { this.$t('m.eform.formDesign.label.dian-ji-xia-zai') } < /a></span >
                < /div>
              )
            })
          } else {
            this.messageUtil.success(res.message)
          }
        } else {
          this.messageUtil.warning(res.message)
        }
        this.loadData()
        this.onClearSelected()
      }).finally(() => {
        this.loading = false
      })
    }
  }
}
</script>
<style lang="less" scoped>

</style>
